<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Section 8.2.2: Separating polyhedra in 2D</title>
<link rel="canonical" href="/Users/mcgrant/Repos/CVX/examples/cvxbook/Ch08_geometric_probs/html/separate_poly_2D.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Section 8.2.2: Separating polyhedra in 2D</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#plots">Plots</a>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Boyd &amp; Vandenberghe "Convex Optimization"</span>
<span class="comment">% Joelle Skaf - 10/09/05</span>
<span class="comment">% (a figure is generated)</span>
<span class="comment">%</span>
<span class="comment">% If the two polyhedra C = {x | A1*x &lt;= b1} and D = {y | A2*y &lt;= b2} can be</span>
<span class="comment">% separated by a hyperplane, it will be of the  form</span>
<span class="comment">%           z'*x - z'*y &gt;= -lambda'*b1 - mu'*b2 &gt; 0</span>
<span class="comment">% where z, lambda and mu are the optimal variables of the problem:</span>
<span class="comment">%           maximize    -b1'*lambda - b2'*mu</span>
<span class="comment">%               s.t.    A1'*lambda + z = 0</span>
<span class="comment">%                       A2'*mu  - z = 0</span>
<span class="comment">%                       norm*(z) &lt;= 1</span>
<span class="comment">%                       lambda &gt;=0 , mu &gt;= 0</span>
<span class="comment">% Note: here x is in R^2</span>

<span class="comment">% Input data</span>
randn(<span class="string">'seed'</span>,0);
n  = 2;
m = 2*n;
A1 = [1 1; 1 -1; -1 1; -1 -1];
A2 = [1 0; -1 0; 0 1; 0 -1];
b1 = 2*ones(m,1);
b2 = [5; -3; 4; -2];

<span class="comment">% Solving with CVX</span>
fprintf(1,<span class="string">'Finding a separating hyperplane between the 2 polyhedra...'</span>);

cvx_begin
    variables <span class="string">lam(m)</span> <span class="string">muu(m)</span> <span class="string">z(n)</span>
    maximize ( -b1'*lam - b2'*muu)
    A1'*lam + z == 0;
    A2'*muu - z == 0;
    norm(z) &lt;= 1;
    -lam &lt;=0;
    -muu &lt;=0;
cvx_end

fprintf(1,<span class="string">'Done! \n'</span>);

<span class="comment">% Displaying results</span>
disp(<span class="string">'------------------------------------------------------------------'</span>);
disp(<span class="string">'The distance between the 2 polyhedra C and D is: '</span> );
disp([<span class="string">'dist(C,D) = '</span> num2str(cvx_optval)]);

<span class="comment">% Plotting</span>
t = linspace(-3,6,100);
p = -z(1)*t/z(2) + (muu'*b2 - lam'*b1)/(2*z(2));
figure;
fill([-2; 0; 2; 0],[0;2;0;-2],<span class="string">'b'</span>, [3;5;5;3],[2;2;4;4],<span class="string">'r'</span>)
axis([-3 6 -3 6])
axis <span class="string">square</span>
hold <span class="string">on</span>;
plot(t,p)
title(<span class="string">'Separating 2 polyhedra by a hyperplane'</span>);
</pre>
<a id="output"></a>
<pre class="codeoutput">
Finding a separating hyperplane between the 2 polyhedra... 
Calling Mosek 9.1.9: 12 variables, 5 equality constraints
------------------------------------------------------------

MOSEK Version 9.1.9 (Build date: 2019-11-21 11:32:15)
Copyright (c) MOSEK ApS, Denmark. WWW: mosek.com
Platform: MACOSX/64-X86

Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : CONIC (conic optimization problem)
  Constraints            : 5               
  Cones                  : 1               
  Scalar variables       : 12              
  Matrix variables       : 0               
  Integer variables      : 0               

Optimizer started.
Presolve started.
Linear dependency checker started.
Linear dependency checker terminated.
Eliminator started.
Freed constraints in eliminator : 0
Eliminator terminated.
Eliminator - tries                  : 1                 time                   : 0.00            
Lin. dep.  - tries                  : 1                 time                   : 0.00            
Lin. dep.  - number                 : 0               
Presolve terminated. Time: 0.00    
Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : CONIC (conic optimization problem)
  Constraints            : 5               
  Cones                  : 1               
  Scalar variables       : 12              
  Matrix variables       : 0               
  Integer variables      : 0               

Optimizer  - threads                : 8               
Optimizer  - solved problem         : the primal      
Optimizer  - Constraints            : 4
Optimizer  - Cones                  : 1
Optimizer  - Scalar variables       : 11                conic                  : 3               
Optimizer  - Semi-definite variables: 0                 scalarized             : 0               
Factor     - setup time             : 0.00              dense det. time        : 0.00            
Factor     - ML order time          : 0.00              GP order time          : 0.00            
Factor     - nonzeros before factor : 10                after factor           : 10              
Factor     - dense dim.             : 0                 flops                  : 8.60e+01        
ITE PFEAS    DFEAS    GFEAS    PRSTATUS   POBJ              DOBJ              MU       TIME  
0   1.0e+00  5.0e+00  2.0e+00  0.00e+00   0.000000000e+00   -1.000000000e+00  1.0e+00  0.00  
1   3.9e-01  2.0e+00  7.7e-01  -2.28e-01  -1.913219675e+00  -2.202778981e+00  3.9e-01  0.01  
2   7.5e-02  3.8e-01  6.3e-02  6.30e-01   -2.617880098e+00  -2.707045863e+00  7.5e-02  0.01  
3   1.0e-02  5.0e-02  3.1e-03  9.27e-01   -2.148987477e+00  -2.160441677e+00  1.0e-02  0.01  
4   4.0e-04  2.0e-03  2.6e-05  9.88e-01   -2.122427666e+00  -2.122892104e+00  4.0e-04  0.01  
5   8.7e-06  4.4e-05  8.2e-08  1.00e+00   -2.121345336e+00  -2.121355382e+00  8.7e-06  0.01  
6   1.9e-07  9.6e-07  2.7e-10  1.00e+00   -2.121320938e+00  -2.121321158e+00  1.9e-07  0.01  
7   4.0e-09  2.0e-08  8.0e-13  1.00e+00   -2.121320357e+00  -2.121320361e+00  4.0e-09  0.01  
Optimizer terminated. Time: 0.01    


Interior-point solution summary
  Problem status  : PRIMAL_AND_DUAL_FEASIBLE
  Solution status : OPTIMAL
  Primal.  obj: -2.1213203566e+00   nrm: 1e+00    Viol.  con: 1e-12    var: 4e-09    cones: 0e+00  
  Dual.    obj: -2.1213203612e+00   nrm: 4e+00    Viol.  con: 0e+00    var: 4e-08    cones: 0e+00  
Optimizer summary
  Optimizer                 -                        time: 0.01    
    Interior-point          - iterations : 7         time: 0.01    
      Basis identification  -                        time: 0.00    
        Primal              - iterations : 0         time: 0.00    
        Dual                - iterations : 0         time: 0.00    
        Clean primal        - iterations : 0         time: 0.00    
        Clean dual          - iterations : 0         time: 0.00    
    Simplex                 -                        time: 0.00    
      Primal simplex        - iterations : 0         time: 0.00    
      Dual simplex          - iterations : 0         time: 0.00    
    Mixed integer           - relaxations: 0         time: 0.00    

------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +2.12132
 
Done! 
------------------------------------------------------------------
The distance between the 2 polyhedra C and D is: 
dist(C,D) = 2.1213
</pre>
<a id="plots"></a>
<div id="plotoutput">
<img src="separate_poly_2D__01.png" alt=""> 
</div>
</div>
</body>
</html>